package com.ld.rightsSystem.controller;


import com.ld.rightsSystem.Enum.ResponseEnum;
import com.ld.rightsSystem.exception.DBDataNotFoundException;
import com.ld.rightsSystem.pojo.TPermission;
import com.ld.rightsSystem.service.inf.TPermissionService;
import com.ld.rightsSystem.vo.R;
import net.bytebuddy.implementation.bind.annotation.Default;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author lhb
 * @since 2023-12-27
 */
@RestController
@RequestMapping("/admin/permission")
public class TPermissionController {

    @Autowired
    private TPermissionService tPermissionService;

    /**
     * 添加菜单
     * @param tPermission
     * @return
     */
    @PostMapping("/add")
    public R addTPermission(@RequestBody TPermission tPermission){
        return tPermissionService.addTPermission(tPermission);
    }

    /**
     * 修改权限
     * @param tPermission
     * @return
     */
    @PutMapping("/update")
    public R editTPermission(@RequestBody TPermission tPermission) throws DBDataNotFoundException {
        return tPermissionService.editTPermission(tPermission);
    }

    /**
     * 根据id删除权限
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public R deleteTPermissionById(@RequestParam("permissionId") Long id) throws DBDataNotFoundException {
        return tPermissionService.deleteTPermissionById(id);
    }

    /**
     * 查询
     * @param permissionId
     * @param pageNum
     * @param pageSize
     * @param tree
     * @return
     */
    @GetMapping("/query")
    public R getTPermissionList(@RequestParam(value = "permissionId",required = false)Long permissionId,
                                @RequestParam(value = "pageNum",required = false)Integer pageNum,
                                @RequestParam(value = "pageSize",required = false)Integer pageSize,
                                @RequestParam(value = "tree",required = false)boolean tree,
                                @RequestParam(value = "checkPName",required = false) String checkPName,
                                @RequestParam(value = "checkPCode",required = false) String checkPCode
                                ) throws DBDataNotFoundException {
        if(permissionId != null){
            return tPermissionService.findTPermissionInfoById(permissionId);
        }else {
            return new R().error(ResponseEnum.PARAMS_BINDING_ERROR);
        }
    }

    @GetMapping("/query/checkPName")
    public R checkPermissionNameAvailable(@RequestParam(value = "checkPName",required = false) String checkPName,
                                          @RequestParam(value = "checkPCode",required = false) String checkPCode){
        return new R().OK(tPermissionService.checkFieldAvailable(checkPName,checkPCode));
    }

    @GetMapping("/query/list")
    public R findTPermissionList(@RequestParam(value = "pageNum",required = false)Integer pageNum,
                                 @RequestParam(value = "pageSize",required = false)Integer pageSize,
                                 @RequestParam(value = "tree",required = false)boolean tree){
        return tPermissionService.findTPermissionList(pageNum,pageSize,tree);
    }

    @GetMapping("/query/info")
    public R findTPermissionList(@RequestParam(value = "pageNum",required = false)Integer pageNum,
                                 @RequestParam(value = "pageSize",required = false)Integer pageSize,
                                 @RequestParam(value = "tree",required = false)boolean tree){
        return tPermissionService.findTPermissionList(pageNum,pageSize,tree);
    }
}

